/***************************************************************************
                           main.c  -  description
                             -------------------
    begin             : 08.04.2008
    copyright         : (C) 2008 by MHS-Elektronik GmbH & Co. KG, Germany
    autho             : Klaus Demlehner, klaus@mhs-elektronik.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software, you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License           *
 *   version 2.1 as published by the Free Software Foundation.             *
 *                                                                         *
 ***************************************************************************/
#include <gtk/gtk.h>
#include "octopus.h"
#include "dialogs.h"
#include "paths.h"
#include "gtk_util.h"
#include "abaut_dlg.h"
#include "setup.h"
#include "gui.h"


GtkWidget *MainWin;
struct octopus_context Octopus;
char  Octopus_Desc[64] = {0};


int OctopusOpen(void)
{
if (octopus_init(&Octopus) < 0)
  {
  msg_box(MSG_TYPE_ERROR, "Error", "error octopus_init:%s", Octopus.error_str);
  return(-1);
  }
if (octopus_open (&Octopus) < 0)		
  {
  msg_box(MSG_TYPE_ERROR, "Error", "error octopus_open: %s", Octopus.error_str);
  return(-1);
  }
if (octopus_get_hwdesc (&Octopus, Octopus_Desc) < 0)
  {
  msg_box(MSG_TYPE_ERROR, "Error", "ERROR octopus_get_hwdesc: %s", Octopus.error_str);  
  return(-1);
  }
return(0);
}


int OctopusSetIoConfig(void)
{
int i, pin;

if (octopus_adc_ref(&Octopus, Config.AdRef) < 0) //AVCC
  {
  msg_box(MSG_TYPE_ERROR, "Error", "ERROR octopus_adc_ref: %s", Octopus.error_str);
  return(-1);
  } 
for (i = 0; i < IO_PIN_COUNT; i++)
  {
  pin = IoPinDefs[i].Pin;
  if ((IoPin[i].Mode == 0) || (IoPin[i].Mode == 1))
    {
    if (octopus_io_init(&Octopus, pin) < 0)
      {
      msg_box(MSG_TYPE_ERROR, "Error", "ERROR (PIN:%u) octopus_io_init: %s", pin, Octopus.error_str);
      return(-1);
      }
    } 
  if (IoPin[i].Mode == 0)
    {
    if (octopus_io_set_pin_direction_in(&Octopus, pin) < 0)
      {
      msg_box(MSG_TYPE_ERROR, "Error", "ERROR (PIN:%u) octopus_io_set_pin_direction_in: %s", pin, Octopus.error_str);
      return(-1);
      }
    }
  else if (IoPin[i].Mode == 1)
    {
    if (IoPinDefs[i].Flags & PIN_A_OUT)
      octopus_pwm_deinit(&Octopus, pin); 
    if (octopus_io_set_pin_direction_out(&Octopus, pin) < 0)
      {
      msg_box(MSG_TYPE_ERROR, "Error", "ERROR (PIN:%u) octopus_io_set_pin_direction_out: %s", pin, Octopus.error_str);
      return(-1);
      }
    } 
  else if (IoPin[i].Mode == 2)
    {
    if (IoPinDefs[i].Flags & PIN_A_OUT)              
      {
      if (octopus_pwm_init(&Octopus, pin) < 0)
        {
        msg_box(MSG_TYPE_ERROR, "Error", "ERROR (PIN:%u) octopus_pwm_init: %s", pin, Octopus.error_str);
        return(-1);
        }
      if (pin == 9)
        {
        if (octopus_pwm_speed(&Octopus, 9, Config.PwmSpeed1) < 0)
          {
          msg_box(MSG_TYPE_ERROR, "Error", "ERROR (PIN:%u) octopus_pwm_speed: %s", pin, Octopus.error_str);
          return(-1);
          }
        }
      else
        {
        if (octopus_pwm_speed(&Octopus, pin, Config.PwmSpeed2) < 0)
          {
          msg_box(MSG_TYPE_ERROR, "Error", "ERROR (PIN:%u) octopus_pwm_speed: %s", pin, Octopus.error_str);
          return(-1);
          }
        }
      }
    }
  else
    {
    if (IoPinDefs[i].Flags & PIN_A_IN)
      {
      if (octopus_adc_init(&Octopus, pin) < 0)
        {
        msg_box(MSG_TYPE_ERROR, "Error", "ERROR (PIN:%u) octopus_adc_init: %s", pin, Octopus.error_str);
        return(-1);
        }
      }
    }
  }
return(0);
}


void OctopusClose(void)
{
(void)octopus_close(&Octopus);
}



int main (int argc, char *argv[])
{
int err;

//gtk_set_locale();
gtk_init(&argc, &argv);
PathsInit(argv[0]);   
GuiInit();

MainWin = CreateMainWin();
gtk_widget_show(MainWin);

if ((err = OctopusOpen()) >= 0)  
  err = OctopusSetIoConfig(); 

if (!err)    
  {
  if (argc >= 2)
    {
    if (!LoadConfigFile(argv[1]))
      {
      save_free(CfgFileName);
      CfgFileName = g_strdup(argv[1]);
      SetConfigToGui();
      SetGuiShowing(0);
      (void)OctopusSetIoConfig();
      }
    }
  //Start counting, refreshing every second
  RefreshTimerId = g_timeout_add(250, RefreshMainWin, NULL);

  gtk_main();
  OctopusClose();
  }
GuiDestroy();
PathsFree();
return(0);
}
